<?php

namespace App\Models\Rbac;

use App\Models\BaseModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends BaseModel
{
    use HasFactory;
    //角色表
    protected $table="role";
    protected $fillable = [
        "name",
        "is_del",
        "created_at",
        "updated_at"
    ];
    public function scopeSearch($query,$params)
    {

        if(!empty($params['name'])){
            $query->where('name',$params['name']);
        }
        $query->where('id','<>',1)->where('is_del',2);
        return $query;
    }

    //获取角色权限
    public function node(){
        return $this->belongsToMany(Node::class,'role_node','role_id','node_id')
            ->where('node.is_menu',1);
    }

    public function nodeAndButton(){
        return $this->belongsToMany(Node::class,'role_node','role_id','node_id');
    }

    public function button(){
        return $this->belongsToMany(Node::class,'role_node','role_id','node_id')
            ->where('node.is_menu',2);
    }

    //获取角色用户
    public function user(){
        return $this->belongsToMany(User::class,'user_role','role_id','user_id')
            ->where('user.is_del',2);
    }
}
